package com.yang.servlet;

import com.google.gson.Gson;
import com.yang.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/Menu2SateChangeServlet")
public class Menu2SateChangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MenuService menuService = new MenuService();
//        Gson gson = new Gson();
        //接收要修改的menuCode和要做的操作
        String menuCode = request.getParameter("menuCode");
        String oper = request.getParameter("oper");
        boolean b = false;
        if("stop".equals(oper)){
            //停用
            try {
                b = menuService.disableMenu(menuCode);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else if ("start".equals(oper)) {
            //启用
            try {
                b = menuService.enableMenu(menuCode);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        String jsonStr = b?"{\"code\":1000,\"msg\":\"success\"}":"{\"code\":1001,\"msg\":\"fail\"}";
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println(jsonStr);
        writer.flush();
        writer.close();


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
